<?php
namespace Home\Controller;
class FlowController extends CommonController {

	public function addToCar(){
		$goodsId=I('goods_id');
		$goodsAttr=I('goods_attr');
		$goodsNum=I('number');
		sort($goodsAttr);
		$goodsAttr=implode(',',$goodsAttr);
		$car = D('Car');
		$car->addToCar($goodsId,$goodsAttr,$goodsNum);
		$this->success("加入购物车成功，正在跳转》》",U("flow1"));
	}
	
	public function flow1(){
		$mid=session('id');
		$mid=intval($mid);
		if(!$mid){
			session("returnURL","./home/flow/flow2");
			$this->error("请先登录！",U("User/login"));
		}
		$car=D('Car');
		$goodsInfo=$car->getGoodsInfo();
		$this->assign('goodsInfo',$goodsInfo);
		$this->display();
	}
	
	public function flow2(){
		if(!session('id')){
			session("returnURL","./home/flow/flow2");
			$this->error("请先登录！",U("User/login"));
		}
		$car=D('Car');
		$goodsInfo=$car->getGoodsInfo();
		$mid = session('id');
		$shrInfos = D('shrinfo')->where(array('mid'=>$mid))->find();
		$this->assign(array(
									'goodsInfo'=>$goodsInfo,
									'shrInfos'=>$shrInfos
						));
		$this->display();
	}
	
	public function flow3(){

		if(!session('id')){
			session("returnURL","./home/flow/flow2");
			$this->error("请先登录！",U("User/login"));
		}
		//检查订单是否有商品
		$car = D("Car");
		$cardata = $car->getGoodsInfo();
		
		if(!count($cardata)){
			$this->error("购物车没有商品数据！",U("flow2"));
		}
		//php文件锁
		$fp = fopen('./order.lock','r');
		flock($fp,LOCK_EX);     //排他锁
		//检查商品库存数量
		$tweight = 0;//商品的总重量
		foreach ($cardata as $k=>$v){
			$tweight+=$v['goods_weight'];
			$gn = D("goods")->getGoodsNum($v['goods_id'],$v['goods_attr_ids']);
			
			if($v['number']>$gn){
				$this->error('商品名为：'.$v['goodsName'].'商品属性为：'.$v['attrStr'].'的商品数量不足，请减少购买数量');
			}
		}
		//处理订单
		//1，收货地址
		
		$data = I('post.');
		//快递运费计算
		$yunfei = yfjs($tweight,$data['peisong'],$data['province'],$data['city'],$data['county']);
		
		//余额支付
		
		$torder = $data['gtprice']+$yunfei;//订单总费用
		$pay_status = 0;
		if($data['pay']=='余额'){
			D('member')->field('money')->find(session('id'));
			$yue = D('member')->money;//获得会员帐户的余额金额
			if($yue>=$torder){
				D('member')->where(array('mid'=>session('id')))->setDec('money',$torder);
				$pay_status = 1;
			}else{
				$this->error("余额不足，请充值！");
			}
		}
		
		$shrInfo = D('shrinfo');
		$mid = session('id');
		$shrInfos = $shrInfo ->where(array('mid'=>$mid))->find();
		if(!$shrInfos){
			$shrInfo->add(array(
				'shr'=>$data['shr'],
				'province'=>$data['province'],
				'city'=>$data['city'],
				'county'=>$data['county'],
				'adress'=>$data['adress'],
				'phone'=>$data['phone'],
				'mid'=>$mid
			));
		}else{
			$shrInfo->where(array('mid'=>$mid))->save(array(
					'shr'=>$data['shr'],
					'province'=>$data['province'],
					'city'=>$data['city'],
					'county'=>$data['county'],
					'adress'=>$data['adress'],
					'phone'=>$data['phone'],
					));
		}
		
		//2，订单基本信息
		$order = D('order');
		$order_id = $order->add(array(
									'sn'=>time().rand(111111,999999),
									'addtime'=>time(),
									'shr'=>$data['shr'],
									'province'=>$data['province'],
									'city'=>$data['city'],
									'county'=>$data['county'],
									'adress'=>$data['adress'],
									'phone'=>$data['phone'],
									'mid'=>$mid,
									'peisong'=>$data['peisong'],
									'pay'=>$data['pay'],
									'pay_status'=>$pay_status,
									'gtprice'=>$data['gtprice'],//商品总的价格
									'yprice'=>$yunfei,//运费的价格
									'tprice'=>$torder,//商品和运费的总共价格
								));
		//，订单的商品信息
			if($order_id){
				$car = D('car');
				$cardata = $car->getGoodsInfo();
				$orderGoods = D('order_goods');
				foreach($cardata as $k => $v){
					$orderGoods->add(array(
							'goods_id'=>$v['goods_id'],
							'goods_name'=>$v['goodsname'],
							'goods_attr_id'=>$v['goods_attr_ids'],
							'goods_attr_str'=>$v['attrStr'],
							'goods_price'=>$v['mprice'],
							'goods_marktprice'=>$v['marketPrice'],
							'goods_num'=>$v['number'],
							'order_id'=>$order_id,
							
					));
				}
				$car = D('car');
				$car->clearCar();
			}
			
			//减少商品库存量
		//	if($this->config['delpro']=='下单时'){	
					foreach($cardata as $k =>$v){
						D('product')->where(array('goods_id'=>$v['goods_id'],'goods_attr'=>$v['goods_attr_ids']))->setDec('goods_number',$v['number']);
					}
			//}
			//释放锁
			flock($fp,LOCK_UN);
			fclose($fp);
			$this->success('下单成功！',U('flow4',array('order_id'=>$order_id,'torder'=>$torder)));
	}
	
	public function flow4($order_id,$torder){
		if(!session('id')){
			$this->error('请先登录！',U('User/login'));
		}
		$order = D('order')->field('pay,pay_status')->find($order_id);
		//dump($order);die;
		if($order['pay'] == '支付宝' && $order['pay_status'] == 0){
			include("./alipay/pagepay/pagepay.php");
		}
		$this->display();
	}
	
	public function ajaxupdategoodsGN($gi,$gattr="",$num=0){
		$car=D('Car');
		if($num==0){
			$car->delGoods($gi,$gattr);
		}else{
			$car->uploadGoodsNum($gi,$gattr,$num);
		}
	}
	
	public function clearCart(){
		echo "111111111111111111";
	//	die();
		$car=D("Car");
		$car->clearrCar();
	}
	
}